/**
 * Created by zhangxiaoyu on 16/12/07.
 */
angular.module('hyt.myOrderService', ['ngResource'])

  .factory('Orders', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getOrders,
      orderData={},pageSize=10;

    var resource = $resource(ApiUrl+':orderSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getOrderData : function(orderSearch){
        var hasNextPage = true;
        resource.query({
          orderSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            parameter:orderSearch.parameter,
            userMsg:orderSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            orderData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('OrderList.Updated');
        })
      },
      getResult:function(){
        if(orderData[pageSize]===undefined){
          return false;
        }
        return orderData[pageSize].data;
      },
      getMoreOrderData : function(orderSearch){
        if(orderData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = orderData[pageSize].hasNextPage;
        var nextPage = orderData[pageSize].nextPage;
        var moreOrderData = orderData[pageSize].data;
        resource.query({
          orderSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            parameter:orderSearch.parameter,
            userMsg:orderSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreOrderData = moreOrderData.concat(r.rows);

          orderData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreOrderData
          };
          $rootScope.$broadcast('OrderList.Updated');
        })
      },
      hasNextPage: function() {
        if (orderData[pageSize] === undefined) {
          return false;
        }
        return orderData[pageSize].hasNextPage;
      }
    }
  })
  .factory('OperationOrder', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.changeOrderStatus,
      result;

    var resource = $resource(ApiUrl+':orderOperation',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getOperationOrder : function(orderOperation){
        return resource.query({
          orderOperation:orderOperation
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('OperationOrder.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('WaybillsOfTrans', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getWaybillsOfTrans,
      waybillsOfTransData={},pageSize=10;

    var resource = $resource(ApiUrl+':transWaybillSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWaybillsOfTrans : function(transWaybillSearch){
        var hasNextPage = true;
        resource.query({
          transWaybillSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            ORDER_TYPE:'2',
            ID:transWaybillSearch.ID,
            userMsg:transWaybillSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            waybillsOfTransData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WaybillsOfTrans.Updated');
        })
      },
      getResult:function(){
        if(waybillsOfTransData[pageSize]===undefined){
          return false;
        }
        return waybillsOfTransData[pageSize].data;
      },
      getMoreWaybillsOfTransData : function(transWaybillSearch){
        if(waybillsOfTransData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = waybillsOfTransData[pageSize].hasNextPage;
        var nextPage = waybillsOfTransData[pageSize].nextPage;
        var moreWaybillsOfTransData = waybillsOfTransData[pageSize].data;
        resource.query({
          transWaybillSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            ORDER_TYPE:'2',
            ID:transWaybillSearch.ID,
            userMsg:transWaybillSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreWaybillsOfTransData = moreWaybillsOfTransData.concat(r.rows);

          waybillsOfTransData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreWaybillsOfTransData
          };
          $rootScope.$broadcast('WaybillsOfTrans.Updated');
        })
      },
      hasNextPage: function() {
        if (waybillsOfTransData[pageSize] === undefined) {
          return false;
        }
        return waybillsOfTransData[pageSize].hasNextPage;
      }
    }
  })
  .factory('WayDetailOfTrans', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getWayDetailOfTrans,
      result;

    var resource = $resource(ApiUrl+':wayDetailOfTrans',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWayDetailOfTrans : function(wayDetailOfTrans){
        return resource.query({
          wayDetailOfTrans:wayDetailOfTrans
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WayDetailOfTrans.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('WayDetailOfOrder', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getWayDetail,
      result;

    var resource = $resource(ApiUrl+':wayDetail',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWayDetail : function(wayDetail){
        return resource.query({
          wayDetail:wayDetail
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WayDetail.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('AddTransWaybill', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.toWaybillOfTransOrder,
      result;

    var resource = $resource(ApiUrl+':waybillTr',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getAddTransWaybill : function(waybillTr){
        return resource.query({
          waybillTr:waybillTr
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('AddTransWaybill.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
